<?php
namespace app\api\controller;
use app\common\controller\Admin;
class Timming extends Admin{
    public function __construct(){
        parent::__construct();
    }	
    //构造函数
    public function _initialize(){
        parent::_initialize();
		$path = config('template.view_path', APP_PATH . 'admin' . DS . 'view' . DS);
        $this->view->config('view_path', $path);		
		set_time_limit(0);
		$param = input();
		if(!$param['tid'] || !$param['pwd']){
			return $this->error('任务ID和任务密码不能为空');
		}
		$info = model('timming')->where(array('timming_id'=>$param['tid'],'timming_status'=>1))->find();
		if(!$info){
			return $this->error('任务不存在或已禁用');
		}
		if($info['timming_password']!=$param['pwd']){
			return $this->error('任务密码错误');
		}
		$this->info=$info;
		$this->url=$url;
    }	
    public function index(){
		$param = input();
		if($this->info['timming_id']){
			$array['url'] =$this->apiurl($this->info,$param['os']);
			if($param['os']){
				return $this->redirect($array['url'][$param['os']]);
			}
			$array['caiji']=$this->info['timming_cjtime'];
			$array['create']=$this->info['timming_htmltime'];
			if($this->info['timming_htmlaction']){
				parse_str($this->info['timming_htmlaction'],$var);
				$var['tid']=$this->info['timming_id'];
				$var['pwd']=$this->info['timming_password'];
			    $array['html_url']=url('create',$var,true,true);
			}
			$array['type']=$this->info['timming_type'];
			$array['data']=$this->info;
			db('timming')->where('timming_id',$this->info['timming_id'])->setField('timming_uptime',time());
			$this->assign($array);
			return $this->fetch('time_wait');
        }			
    }
    public function api(){
		$this->assign('title','采集');
		$request = controller('admin/collect');
		$request->api();
    }
    public function create(){
        $this->assign('title','生成');		
		$request = controller('admin/html');
		$request->create();
    }	
	public function apiurl($info,$os=false){
		config('url_common_param',true);
		$apiarray=explode(',',$info['timming_apiurl']);
	    foreach($apiarray as $key=>$value){
			parse_str($value,$var);
			$var['apiurl']=base64_encode($var['apiurl']);
			if(!empty($var['param'])){
                $var['param']=base64_encode($var['param']);
			}else{
				unset($var['param']);
			}
            $var['tid']	=$info['timming_id'];
            $var['pwd']	=$info['timming_password'];
            if($os){
				$var['os']=1;
			}		
			$array['url'][$key+1] = url('api',$var,true,true);
		}		
		return $array['url']; 
	}	
}
require APP_PATH.'admin/common.php';
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
